/**
 * @name zhangyy-g@grandsoft.com.cn
 * @description zhangyy's code
 * Date: 15-4-17
 * Time: 下午6:08
 *
 */
var App = {
    modules: {},
    common: {
        modules: {}
    }
};
(function () {
    var util = {
        rootPath: window.location.protocol + '//' + window.location.host,
        filterPath: ['/', '/收到的共享/', '/公司文件/'],
        filterInfo: function (filter) {
            return {
                    myfile: {
                        name: '我的文件',
                        path: '/',
                        action: 'list'
                    },
                    share: {
                        name: '收到的共享',
                        path: '/收到的共享/',
                        action: 'list'
                    },
                    company: {
                        name: '公司文件',
                        path: '/公司文件/',
                        action: 'list'
                    },
                    favorite: {
                        name: '星标文件',
                        action: 'favorite',
                        path: '/'
                    },
                    myshare: {
                        name: '我的共享文件夹',
                        action: 'myshare'
                    }
                }[filter] || {}
        },
        folderAuth: function (file) {

        },
        getUrl: function (options) {
            var rootUrl = '/review/file/'
            var data = {
                p: 0,
                s: 50,
                orderby: 'lastmodifiedtime',
                ordertype: 'desc'
            }
            switch (options.action) {
                case 'search':
                    return {
                        url: rootUrl + 'search/filetext/'
                    }
                case 'list':
                    data.path = options.path
                    return {
                        url: rootUrl + 'list/',
                        data: data
                    }
                case 'favorite':
                    data.path = 'filter:已加星标'
                    return {
                        url: '/webfs/file/favorite/listFavorites/',
                        data: data
                    }
                case 'myshare':
                    data.path = 'filter:我的共享'
                    return {
                        url: '/webfs/file/mysharefolder',
                        data: data
                    }
            }
        },
        suffixes: {
            zi: ['7z', 'arj', 'bz2', 'bzip2', 'cab', 'cpio', 'deb', 'dmg', 'gz', 'gzip', 'hfs', 'iso', 'lha', 'lzh', 'lzma', 'rar', 'rpm', 'split', 'swm', 'tar', 'taz', 'tbz', 'tbz2', 'tgz', 'tpz', 'wim', 'xar', 'z', 'zip'],
            mp: ['mp3', 'wma', 'wav', 'mod', 'md', 'aac', 'flac', 'ape', 'mid', 'ogg', 'm4a'],
            vi: ['wmv', 'asf', 'asx', 'rm', 'rmvb', 'mpg', 'mpeg', 'mpe', '3gp', 'mov', 'mp4', 'm4v', 'avi', 'dat', 'mkv', 'vob'],
            im: ['bmp', 'pcx', 'tiff', 'gif', 'jpeg', 'jpg', 'tga', 'fpx', 'png', 'tif', 'raw', "pjpg", "pbmp", "ppng"],
            ps: ['psd'],
            ai: ['ai', 'eps', 'svg'],
            fl: ['swf', 'flv'],
            wd: ['doc', 'docx'],
            xs: ['xls', 'xlsx'],
            pt: ['ppt', 'pptx'],
            pd: ['pdf']
        },
        setFileIcon: function (file) {
            var fileIcon = 'other'
            file.fileName = file.fileName || file.filename
            file.extension = file.fileName.substr(file.fileName.lastIndexOf('.') + 1)
            var extension = (file.extension && file.extension.toLowerCase()) || ''
            if (file.dir || file.isDir || file.isdir) {
                // maybe app data
                fileIcon = 'folder';

                // '1' - itself is a share folder; '2' - it is a sub share folder
                if (file.shareFolderFlag == "1" || file.shareFolderFlag == "2") {
                    fileIcon = 'share';
                }

                // it is a public folder or a sub public folder
                if (file.iscompanyfolder == "1") {
                    fileIcon = 'public-folder';
                }
                file.dir = 1;
            } else {
                var ext = extension.replace(/\d+$/g, '')
                var suffixes = this.suffixes
                _.each(suffixes, function (item, key) {
                    if (_.contains(item, ext)) {
                        fileIcon = key;
                        return false
                    }
                });
            }
            file.fileIcon = fileIcon
            return file
        },
        isImg: function (file) {
            file.fileName = file.fileName || file.filename
            var extension = file.extension || file.fileName.substr(file.fileName.lastIndexOf('.') + 1)
            extension = extension.toLowerCase()
            return _.contains(this.suffixes.im, extension)
        },
        isOffice: function (file) {
            file.fileName = file.fileName || file.filename
            var ext = file.extension || file.fileName.substr(file.fileName.lastIndexOf('.') + 1)
            ext = ext.toLowerCase()
            return _.contains(this.suffixes.wd, ext) || _.contains(this.suffixes.xs, ext) || _.contains(this.suffixes.pt, ext)
        },
        isPdf: function(file){
            file.fileName = file.fileName || file.filename
            var extension = file.extension || file.fileName.substr(file.fileName.lastIndexOf('.') + 1)
            extension = extension.toLowerCase()
            return extension == 'pdf'
        },
        canUpload: function (file) {
            var auth = file.authority
            if (!auth) return true

            return auth[3 + 5] == 1
        },
        canNewfolder: function (file) {
            var auth = file.authority
            if (!auth) return true

            return auth[3 + 7] == 1
        },
        canShareto: function (file) {
            var auth = file.authority
            if (!auth) return true

            return auth[3 + 9] == 1
        },
        canLink: function (file) {
            var auth = file.authority
            if (!auth) return true

            return auth[3 + 0] == 1 || auth[3 + 6] == 1
        },
        canPreview: function(file) {
            var auth = file.authority
            if (!auth) return true

            return auth[3 + 4] == 1
        },
        isEmail: function (v) {
            return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(v)
        },

        isMobile: function (v) {
            return /^\d{11}$/.test(v)
        }
    }

    App.common.modules.util = util
})(jQuery);

$(function(){
    // 页面适配
    function windowResize(){
        var size = $(this).width();
        if (size < 640) {
            $('html').css('font-size', size/3.2);
            $('body').removeClass('g-maxbody');
        } else {
            $('html').css('font-size', 640/3.2);
            $('body').addClass('g-maxbody');
        }
    }

    $(window).resize(function() {
        windowResize();
    });

    windowResize();

    if(browser.versions.mobile){
        if (browser.versions.ios) {
            $('#tongbupan').before($('<div id="tongbupan-gap"></div>')).height($(window).height() - 10)
        }
    }
});